package net.chenlin.dp.generator.controller;

import net.chenlin.dp.base.entity.DictTableEntity;
import net.chenlin.dp.base.service.DictTableService;
import net.chenlin.dp.common.annotation.SysLog;
import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.JSONUtils;
import net.chenlin.dp.common.utils.JdbcUtils;
import net.chenlin.dp.generator.entity.GeneratorParamEntity;
import net.chenlin.dp.generator.entity.TableEntity;
import net.chenlin.dp.generator.service.SysGeneratorService;
import net.chenlin.dp.generator.utils.VelocityUtils;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.VelocityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author ZhouChenglin
 * @email yczclcn@163.com
 * @url www.chenlintech.com
 * @date 2017年8月28日 下午8:56:30
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {

	@Autowired
	private SysGeneratorService sysGeneratorService;
	@Autowired
	private DictTableService dictTableService;
	
	/**
	 * 表格列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Page<TableEntity> list(@RequestBody Map<String, Object> params) {
		return sysGeneratorService.listTable(params);
	}
	
	/**
	 * 生成代码
	 *
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/code")
	public void generator(GeneratorParamEntity params, HttpServletRequest request, HttpServletResponse response) throws IOException {
		byte[] code = sysGeneratorService.generator(params);
		String attachment = "attachment; filename=" + params.getTables()[0] + ".zip";
		response.reset();  
        response.setHeader("Content-Disposition", attachment);  
        response.addHeader("Content-Length", "" + code.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
        IOUtils.write(code, response.getOutputStream());
	}

	/**
	 * 生成mysql 表结构
	 * @return
	 */
	@SysLog("创建项目表")
	@RequestMapping("/create")
	@ResponseBody
	public R table(@RequestBody String name,String code, Long [] ids){
		String a = "";

		try{
			JdbcUtils jdbcUtils = new JdbcUtils();
			String sql = "select * from dp_table_colum";
			List<Map> tableData = jdbcUtils.selectByParams(sql,null);
			List<DictTableEntity> lists = new ArrayList<>();
			//System.out.println(JSONUtils.beanToJson(tableData))
			for (Map m : tableData){
				DictTableEntity dictTableEntity = new DictTableEntity();
				dictTableEntity.setName(String.valueOf(m.get("name")));
				dictTableEntity.setCode(String.valueOf(m.get("code")));
				dictTableEntity.setCodeType(String.valueOf(m.get("code_type")));
				lists.add(dictTableEntity);
			}

			System.out.println(JSONUtils.beanToJson(lists));
			Map<String,Object> maps = new HashMap<>();
			maps.put("tableName","test");
			maps.put("tableEntity",lists);

			VelocityContext context = new VelocityContext(maps);
			String tableSql = VelocityUtils.getFileForString("template/createTable.vm",context);
			String tmpsql = tableSql.substring(0,tableSql.lastIndexOf(","))+")";

			System.out.println("table sql \n" + tableSql);
			/*String dropSql = "DROP TABLE IF EXISTS `" + maps.get("tableName") +"`";
			jdbcUtils.execute(dropSql);
			jdbcUtils.execute(tmpsql);*/
		}catch (Exception e){

		}


		return new R();
	}
}
